import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录界面
 */
@Setter
@Getter
@NoArgsConstructor
public class Login extends JFrame {
    private List<ChatClient> clientList=new ArrayList<ChatClient>();
    static Login self;
    private Socket socket;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private String userid;// 登陆用户名
    private JLabel unLabel = new JLabel("用户名");// 登陆面板控件
    private JTextField unField = new JTextField();
    private JButton login = new JButton("登录");
    private JTextField warn = new JTextField();


    public Login(Socket socket, ObjectOutputStream oos){
        this.socket = socket;
        this.oos = oos;
        this.self = this;
        System.out.println("进来Login方法===========");
        this.setSize(300, 250);// 设置登陆面板
        Dimension screen = getToolkit().getScreenSize();
        Dimension frame = this.getSize();
        this.setLocation((screen.width - frame.width) / 2, (screen.height - frame.height) / 2);
        this.setVisible(true);
        this.setResizable(false);
        this.setLayout(null);
        this.setTitle("聊天室");

        unLabel.setSize(50, 30);
        unLabel.setLocation(50, 30);
        unField.setSize(120, 30);
        unField.setLocation(110, 30);
        unField.requestFocus();
        login.setSize(80, 30);
        login.setLocation(110, 160);

        warn.setSize(220, 30);
        warn.setLocation(110, 120);
        warn.setForeground(Color.red);
        warn.setEditable(false);
        warn.setBorder(null);
        //warn.requestFocus();

        this.add(unLabel);
        this.add(unField);
        this.add(login);
        this.add(warn);

        //鼠标监听事件
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userid = unField.getText();
                System.out.println(userid);
                if(userid == null || userid.equals("")){
                    System.out.println("请输入用户名！！");
                    new MyDialogDemo(); //弹窗提示
                }
                if (userid != null && !userid.equals("")) {
                    unField.setEditable(false);
                    try {
                        //获取输出流并向服务器端发送 用户名：userid
                        //oos = new ObjectOutputStream(socket.getOutputStream());//客户端的输出流
                        oos.writeObject(userid);//将用户名发送到服务器
                        self.setVisible(false);//关闭登陆界面
                        //ois = new ObjectInputStream(socket.getInputStream());
                        ChatClient client = new ChatClient(socket,oos);//打开聊天界面,并将输入输出流传到ChatClient
                        clientList.add(client);
                        client.crateUI(clientList);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        //窗口监听事件
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    //弹窗的窗口
    private class MyDialogDemo extends JDialog {
        public MyDialogDemo() {
            this.setVisible(true);
            this.setBounds(850, 450, 200, 100);
            //弹窗已经默认可以关闭窗口
            //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            Container container = this.getContentPane();
            container.setLayout(null);
            JLabel label = new JLabel("请输入用户名！");
            label.setSize(100, 50);
            //label.setHorizontalAlignment(SwingConstants.CENTER);
            container.add(label);
        }
    }

    public static void main(String[] args) {

        ObjectOutputStream oos ;
        ObjectInputStream ois ;
        try {
            Socket socket = new Socket("localhost", 9900);
            System.out.println("连接服务器成功");
            oos = new ObjectOutputStream(socket.getOutputStream());
            System.out.println("获取oos成功");
            new Login(socket, oos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
